﻿using Arch.Samples.RiaServices.Commands;
using Arch.Samples.RiaServices.Infrastructure;
using Arch.Client.Commands;
using Arch.Samples.Model;
using Arch.Samples.RiaServices.Services;

namespace Arch.Samples.RiaServices.ViewModels
{
    public class CategoryListViewModel : CollectionViewModelBase<Category>
    {
        public CategoryListViewModel(TodoContext context) : base(context)
        {
            DeleteCommand = new DeleteCommand<CategoryListViewModel, Category>(this, e => Context.Categories.Remove(e));
            DeleteSelectedCommand = new DeleteSelectedCommand<CategoryListViewModel, Category>(this, e => Context.Categories.Remove(e));
            AddCommand = new RelayCommand(() =>
            {
                var c = new GlobalCategory();
                Context.EntityContainer.GetEntitySet<Category>().Add(c);
                Items.Add(c);
            });

            Refresh();
        }

        public void LoadCategories()
        {
            Context.Load(Context.GetCategoryQuery(), l => Items.Reload(l.Entities));
        }

        public override void Refresh()
        {
            LoadCategories();
        }
    }
}
